package ocg.http.httpcore;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenbo on 2018/5/8.
 */
public class Basic {
    private static Logger logger = LogManager.getLogger ( Basic.class );

    private static Map<String,String> headers = new HashMap <> (  );
    private static Map<String,String> cookies = new HashMap <> (  );
    private static String BasicUrl = null;
    private static String Scheme = null;
    private static String Host = null;
    private static String Ip = null;
    private static int Part = 0;

    // 私有构造器，防止类的实例化
    private Basic(){
        super();
    }

    public static void setBasicUrl(String Scheme,String Host){
        Basic.Scheme = Scheme;
        Basic.Host = Host;
    }

    public static void setBasicUrl(String Scheme,String Ip,int Part){
        Basic.Scheme = Scheme;
        Basic.Ip = Ip;
        Basic.Part = Part;
    }

    public static String getBasicUrl() {

        StringBuffer buffer = new StringBuffer ();
        if ( Basic.Host != null ) {
            return buffer.append ( Basic.Scheme ).append ( "://" ).append ( Basic.Host ).toString ();
        } else if ( Ip !=null && Part >0 ){
            return buffer.append ( Basic.Scheme ).append ( "://" )
                    .append ( Basic.Ip )
                    .append ( ":" )
                    .append ( Basic.Part ).toString ();
        }
        return null;
    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        Basic.logger = logger;
    }

    public static Map <String, String> getHeaders() {
        return headers;
    }

    public static void setHeaders(Map <String, String> headers) {
        if (headers.size ()>0) {
            for (String s : headers.keySet ()) {
                Basic.headers.put ( s , headers.get ( s ) );
            }
        }
    }

    public static void addHeaders(Map <String, String> headers) {
        Basic.headers = headers;
    }

    public static void setHeader( String key,String value) {
        Basic.headers.put ( key , value );
    }

    public static Map <String, String> getCookies() {
        return cookies;
    }

    public static void setCookies(Map <String, String> cookies) {
        Basic.cookies = cookies;
    }

    public static void addCookies(Map <String, String> cookies) {
        Basic.cookies = cookies;
    }

    public static String getScheme() {
        return Scheme;
    }

    public static void setScheme(String scheme) {
        Scheme = scheme;
    }

    public static String getHost() {
        return Host;
    }

    public static void setHost(String host) {
        if (host != null && host.equals ( "" )) {
            Basic.Host = host;
        }

    }

    public static String getIp() {
        return Ip;
    }

    public static void setIp(String ip) {
        Ip = ip;
    }

    public static int getPart() {
        return Part;
    }

    public static void setPart(int part) {
        Part = part;
    }
}
